cmake_minimum_required(VERSION 3.5)

project(Chicken)

set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)

# 设置 rpath 参数
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")

if(APPLE)
    set(CMAKE_INSTALL_RPATH "@executable_path/../lib")
elseif(UNIX)
    set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib")
endif()

add_library(chicken_lib SHARED ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib)

add_executable(chicken ${SOURCES})
target_link_directories(chicken PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/lib)
target_link_libraries(chicken chicken_lib)

install(TARGETS chicken DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
install(TARGETS chicken_lib DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION
  ${CMAKE_INSTALL_PREFIX}/include)


# 设置源文件目录
set(SOURCE_DIR ${CMAKE_SOURCE_DIR}/..)

# 添加 Doxygen 构建目标
find_package(Doxygen REQUIRED)

# 设置 Doxygen 配置文件路径
set(DOXYGEN_CONFIG_FILE ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)


# 创建生成 Doxyfile 的自定义构建目标
add_custom_command(
    OUTPUT ${DOXYGEN_CONFIG_FILE}
    COMMAND ${DOXYGEN_EXECUTABLE} -g ${DOXYGEN_CONFIG_FILE}
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    COMMENT "Generating Doxyfile"
    VERBATIM
)

# 将生成 Doxyfile 的自定义构建目标与默认构建目标关联
add_custom_target(generate_doxygen_config ALL DEPENDS ${DOXYGEN_CONFIG_FILE})

# 添加生成文档的自定义构建目标，并依赖于生成 Doxyfile 的构建目标
add_custom_target(
    doc
    COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_CONFIG_FILE}
    COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_CONFIG_FILE}
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    COMMENT "Generating API documentation with Doxygen"
    VERBATIM
)

# 将生成文档的自定义构建目标与默认构建目标关联
add_dependencies(chicken doc)

